/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.kpi.transport.servlet.actions.saving;

import ua.kpi.transport.servlet.actions.lists.StationsListAction;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ua.kpi.transport.db.dao.factory.DAOFactory;
import ua.kpi.transport.db.dao.factory.DBEnum;
import ua.kpi.transport.db.dao.interfaces.IStationDAO;
import ua.kpi.transport.db.dao.interfaces.IStationToTypeDAO;
import ua.kpi.transport.db.dao.interfaces.ITransportTypeDAO;
import ua.kpi.transport.db.entities.StationBean;
import ua.kpi.transport.db.entities.StationToTypeBean;
import ua.kpi.transport.servlet.actions.IAction;
import ua.kpi.transport.util.settings.Pages;

/**
 *
 * @author harker777
 */
public class SaveStationAction implements IAction{

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        IStationDAO sd = DAOFactory.getDAOFactory(DBEnum.MYSQL).getStationDAO();
        
        Integer id = Integer.valueOf(request.getParameter("id"));
        String name = request.getParameter("name");
        Float lattitude = Float.valueOf(request.getParameter("lattitude"));
        Float longitude = Float.valueOf(request.getParameter("longitude"));
        
        StationBean station = sd.find(id);
        station.setName(name);
        station.setLattitude(lattitude);
        station.setLongitude(longitude);
        sd.update(station);
    
        ITransportTypeDAO ttd = DAOFactory.getDAOFactory(DBEnum.MYSQL).getTransportTypeDAO();
        IStationToTypeDAO sttd = DAOFactory.getDAOFactory(DBEnum.MYSQL).getStationToTypeDAO();
        List<StationToTypeBean> types = sd.findFull(station).getTypesList();
        for (StationToTypeBean sttb : types){
            if (request.getParameter("type_" + sttb.getTransportTypeId()) != null){
                sttb.setStatus(true);
                sttd.update(sttb);
            }else{                
                sttb.setStatus(false);
                sttd.update(sttb);
            }            
        }
        
        return new StationsListAction().execute(request, response);
    }
}
